package token

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/services/token"
	"net/http"
)

type UpdateTokenData struct {
	Id          string `bson:"id"`
	ChainName   string `bson:"chainName"`
	ChainId     uint   `bson:"chainId"`
	Address     string `bson:"address"`
	Type        string `bson:"type"` //1.主币 2.代币
	Symbol      string `bson:"symbol"`
	Decimals    uint   `bson:"decimals"`
	TotalSupply string `bson:"totalSupply"`
	Name        string `bson:"name"`
}

func (h *handler) UpdateToken() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(UpdateTokenData)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)),
			)
			return
		}

		createData := new(token.CreateTokenData)
		createData.Id = req.Id
		createData.ChainName = req.ChainName
		createData.ChainID = req.ChainId
		createData.Address = req.Address
		createData.Type = req.Type
		createData.TotalSupply = req.TotalSupply
		createData.Decimals = req.Decimals
		createData.Symbol = req.Symbol
		createData.Name = req.Name

		err := h.tokenService.TokenUpdate(ctx, createData)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ProductCreateError,
				code.Text(code.ProductCreateError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
